package com.automation.entity;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;

import java.nio.file.Paths;
import java.util.List;

public class PublishPage extends BasePage {
    private final By createNoteBtn = By.id("com.dianping.v1:id/create_note");
    private final By addPhotoBtn = By.id("com.dianping.v1:id/add_photo");
    private final By photoItem = By.id("com.dianping.v1:id/photo_item");
    private final By nextStepBtn = By.id("com.dianping.v1:id/btn_next");
    private final By generateCollage = By.id("com.dianping.v1:id/generate_collage");
    private final By topicSelector = By.id("com.dianping.v1:id/topic_selector");
    private final By topicItem = By.id("com.dianping.v1:id/topic_item");
    private final By saveDraftBtn = By.id("com.dianping.v1:id/save_draft");

    public PublishPage(AndroidDriver driver) {
        super(driver);
    }

    public void startCreateNote() {
        click(createNoteBtn);
    }

    public void addPhotos(List<String> imagePaths) {
        click(addPhotoBtn);

        // 选择所有截图
        for (String path : imagePaths) {
            // 实际项目中需要根据文件名定位元素
            By imageLoc = By.xpath(String.format(
                    "//*[@content-desc='%s']",
                    Paths.get(path).getFileName().toString()
            ));
            click(imageLoc);
        }
    }

    public void generateRecommendation() {
        click(nextStepBtn);
        click(generateCollage);
    }

    public void selectTopics(int count) {
        click(topicSelector);

        for (int i = 0; i < Math.min(count, driver.findElements(topicItem).size()); i++) {
            driver.findElements(topicItem).get(i).click();
        }
    }

    public void saveAsDraft() {
        click(saveDraftBtn);
    }
}